package com.tledu.data.export.service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.tledu.data.export.entity.FinalExcel;
import com.tledu.data.export.mapper.ExportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Service
public class ExportService {
    @Autowired
    private ExportMapper exportMapper;
    public List<FinalExcel> find(String name,String starttime,String endtime){
        System.out.println("find方法接收到:"+name+" "+starttime+""+endtime);
        return exportMapper.find(name,starttime,endtime);
    }
    public void export(String name,String starttime,String endtime, HttpServletResponse response) throws IOException {
        // 查询数据库或其他数据源获取导出的数据
        System.out.println("service接收到:"+name+" "+starttime+""+endtime);
        List<FinalExcel> finalExcelList = find(name,starttime,endtime);
        System.out.println("查出的数据:"+finalExcelList);

        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String headers2="attachment;filename="+"export"+".xlsx";
        System.out.println("headers2:"+headers2);
        response.setHeader("Content-disposition", headers2);

        // 导出Excel
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), FinalExcel.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
        excelWriter.write(finalExcelList, writeSheet);
        excelWriter.finish();
    }
}
